#######################################################
# Online Appendix 
########################################################
rm(list=ls())
library("foreign")


###################################################################################
# Table  A.9. The Dampening Effect  Effect of Three Non-proliferation Tools --Threatened respondents
###################################################################################

data<-read.csv("public.csv")
data<-subset(data, data$tperception==1)
data3<-subset(data, data$age==1|data$age==2|data$age==3) #20-30s block
data4<-subset(data, data$age==4|data$age==5|data$age==6) #40-50s block


###Bootstrp function 

B<-10000

bootTreat<- function(dat, label="Treatment"){
  bootResults <- matrix(NA, nrow=B, ncol=3)
  for (i in seq_len(B)){
    resample <- sample(1:nrow(dat),nrow(dat),replace=T)
    temp <- dat[resample,]
    A <- mean(temp$support[which(temp$declaration==1)], na.rm=TRUE)
    B <- mean(temp$support[which(temp$deployment==1)], na.rm=TRUE)
    C <- mean(temp$support[which(temp$threat==1)], na.rm=TRUE)
    D <- mean(temp$support[which(temp$control==1)], na.rm=TRUE)
    bootResults[i,1] <- (A-D)
    bootResults[i,2] <- (B-D)
    bootResults[i,3] <- (C-D)
    drop(list())
  }
  return(list(model=label, boot=bootResults, Declare=mean(bootResults[,1], na.rm=TRUE), Deploy=mean(bootResults[,2], na.rm=TRUE), Threat=mean(bootResults[,3], na.rm=TRUE)))
}


##full sample


mean(data$support[data$control==1])
mean(data$support[data$declaration==1])-mean(data$support[data$control==1])
mean(data$support[data$deploy==1])-mean(data$support[data$control==1])
mean(data$support[data$threat==1])-mean(data$support[data$control==1])


set.seed(1000)
full.p <- bootTreat(data)

length(which(full.p$boot[,1] > 0))/B #p=0.2762: declaration
length(which(full.p$boot[,2] >0))/B #p=0.0818: deployment
length(which(full.p$boot[,3] >0 ))/B #p=0.4367: threat



##20-30s Support 

set.seed(1000)
full.3p <- bootTreat(data3)

mean(data3$support[data3$control==1])
mean(data3$support[data3$declaration==1])-mean(data3$support[data3$control==1])
mean(data3$support[data3$deploy==1])-mean(data3$support[data3$control==1])
mean(data3$support[data3$threat==1])-mean(data3$support[data3$control==1])

length(which(full.3p$boot[,1] > 0))/B #declaration 0.0426
length(which(full.3p$boot[,2] >0))/B #deployment 0.0129
length(which(full.3p$boot[,3] >0 ))/B #threat 0.012


##40-50s Support 



set.seed(1000)
full.4p <- bootTreat(data4)

mean(data3$support[data4$control==1])
mean(data4$support[data4$declaration==1])-mean(data4$support[data4$control==1])
mean(data4$support[data4$deploy==1])-mean(data4$support[data4$control==1])
mean(data4$support[data4$threat==1])-mean(data4$support[data4$control==1])

length(which(full.4p$boot[,1] < 0))/B #declaration 0.200
length(which(full.4p$boot[,2] <0))/B #deployment 0.419
length(which(full.4p$boot[,3] <0 ))/B #threat 0.029



#############################################################################################
# To Create Figure A.1 and A.2 calculate the difference in means and confidence intervals
#############################################################################################

### Bootstrap Functions

B<-10000

bootTreat2 <- function(dat, label="Treatment"){
  bootResults <- matrix(NA, nrow=B, ncol=2)
  for (i in seq_len(B)){
    resample <- sample(1:nrow(dat),nrow(dat),replace=T)
    temp <- dat[resample,]
    A <- mean(temp$support[which(temp$declaration==1)], na.rm=TRUE)
    B <- mean(temp$support[which(temp$deployment==1)], na.rm=TRUE)
    C <- mean(temp$support[which(temp$threat==1)], na.rm=TRUE)
    D <- mean(temp$support[which(temp$control==1)], na.rm=TRUE)
    bootResults[i,1] <- (B-A)
    bootResults[i,2] <- (C-A)
    drop(list())
  }
  return(list(model=label, boot=bootResults, Deploy=mean(bootResults[,1], na.rm=TRUE), Threat=mean(bootResults[,2], na.rm=TRUE)))
}


bootCredit2 <- function(dat, label="Treatment"){
  bootResults <- matrix(NA, nrow=B, ncol=2)
  for (i in seq_len(B)){
    resample <- sample(1:nrow(dat),nrow(dat),replace=T)
    temp <- dat[resample,]
    A <- mean(temp$credibility[which(temp$declaration==1)], na.rm=TRUE)
    B <- mean(temp$credibility[which(temp$deployment==1)], na.rm=TRUE)
    C <- mean(temp$credibility[which(temp$threat==1)], na.rm=TRUE)
    bootResults[i,1] <- (B-A)
    bootResults[i,2] <- (C-A)
    drop(list())
  }
  return(list(model=label, boot=bootResults, Deploy=mean(bootResults[,1], na.rm=TRUE), Threat=mean(bootResults[,2], na.rm=TRUE)))
}



####20s-30s block 

##1) support for nuclearization 


mean(data3$support[data3$deploy==1])-mean(data3$support[data3$declaration==1]) 
mean(data3$support[data3$threat==1])-mean(data3$support[data3$declaration==1]) 

##calculate confidence intervals 
set.seed(1000)
full.3pr <- bootTreat2(data3)

p3d<-round(c(full.3pr$Deploy, quantile(full.3pr$boot[,1], c(0.025, 0.975))), digits=2)
p3d  

p3t<-round(c(full.3pr$Threat, quantile(full.3pr$boot[,2], c(0.025, 0.975))), digits=2)
p3t 

p3d90<-round(c(full.3pr$Deploy, quantile(full.3pr$boot[,1], c(0.05, 0.95))), digits=2)
p3d90 

p3t90<-round(c(full.3pr$Threat, quantile(full.3pr$boot[,2], c(0.05, 0.95))), digits=2)
p3t90 


##2) credibility of extended deterrence 
mean(data3$credibility[data3$declaration==1]) 

mean(data3$credibility[data3$deploy==1])-mean(data3$credibility[data3$declaration==1]) 
mean(data3$credibility[data3$threat==1])-mean(data3$credibility[data3$declaration==1]) 


#calculate confidence intervals 
set.seed(1000)
full.3pc<-bootCredit2(data3)


c3d<-round(c(full.3pc$Deploy, quantile(full.3pc$boot[,1], c(0.025, 0.975))), digits=2)
c3d 

c3t<-round(c(full.3pc$Threat, quantile(full.3pc$boot[,2], c(0.025, 0.975))), digits=2)
c3t 

c3d90<-round(c(full.3pc$Deploy, quantile(full.3pc$boot[,1], c(0.05, 0.95))), digits=2)
c3d90 

c3t90<-round(c(full.3pc$Threat, quantile(full.3pc$boot[,2], c(0.05, 0.95))), digits=2)
c3t90 



####40s-50s Block 



##1) support for nuclearization 


mean(data4$support[data4$deploy==1])-mean(data4$support[data4$declaration==1]) 
mean(data4$support[data4$threat==1])-mean(data4$support[data4$declaration==1]) 


###calculate confidence intervals
set.seed(1000)
full.4pr <- bootTreat2(data4)

p4d<-round(c(full.4pr$Deploy, quantile(full.4pr$boot[,1], c(0.025, 0.975))), digits=2)
p4d

p4t<-round(c(full.4pr$Threat, quantile(full.4pr$boot[,2], c(0.025, 0.975))), digits=2)
p4t

p4d90<-round(c(full.4pr$Deploy, quantile(full.4pr$boot[,1], c(0.05, 0.95))), digits=2)
p4d90 

p4t90<-round(c(full.4pr$Threat, quantile(full.4pr$boot[,2], c(0.05, 0.95))), digits=2)
p4t90 



##2) credibility of extended deterrence 
mean(data4$credibility[data4$declaration==1])

mean(data4$credibility[data4$deploy==1])-mean(data4$credibility[data4$declaration==1]) 
mean(data4$credibility[data4$threat==1])-mean(data4$credibility[data4$declaration==1]) 


###calculate confidence intervals
set.seed(1000)
full.4pc<-bootCredit2(data4)

c4d<-round(c(full.4pc$Deploy, quantile(full.4pc$boot[,1], c(0.025, 0.975))), digits=2)
c4d

c4t<-round(c(full.4pc$Threat, quantile(full.4pc$boot[,2], c(0.025, 0.975))), digits=2)
c4t 

c4d90<-round(c(full.4pc$Deploy, quantile(full.4pc$boot[,1], c(0.05, 0.95))), digits=2)
c4d90 

c4t90<-round(c(full.4pc$Threat, quantile(full.4pc$boot[,2], c(0.05, 0.95))), digits=2)
c4t90 




### full sample 



##1) support for nuclearization 


mean(data$support[data$deploy==1])-mean(data$support[data$declaration==1]) 
mean(data$support[data$threat==1])-mean(data$support[data$declaration==1]) 


##calculate confidence intervals
set.seed(1000)
full.pr <- bootTreat2(data)


pd<-round(c(full.pr$Deploy, quantile(full.pr$boot[,1], c(0.025, 0.975))), digits=2)
pd

pt<-round(c(full.pr$Threat, quantile(full.pr$boot[,2], c(0.025, 0.975))), digits=2)
pt

pd90<-round(c(full.pr$Deploy, quantile(full.pr$boot[,1], c(0.05, 0.95))), digits=2)
pd90

pt90<-round(c(full.pr$Threat, quantile(full.pr$boot[,2], c(0.05, 0.95))), digits=2)
pt90



##2) credibility of extended deterrence 
mean(data$credibility[data$declaration==1]) 

mean(data$credibility[data$deploy==1])-mean(data$credibility[data$declaration==1]) 
mean(data$credibility[data$threat==1])-mean(data$credibility[data$declaration==1]) 


###calculate confidence intervals
set.seed(1000)
full.pc<-bootCredit2(data)

cd<-round(c(full.pc$Deploy, quantile(full.pc$boot[,1], c(0.025, 0.975))), digits=2)
cd 
ct<-round(c(full.pc$Threat, quantile(full.pc$boot[,2], c(0.025, 0.975))), digits=2)
ct
cd90<-round(c(full.pc$Deploy, quantile(full.pc$boot[,1], c(0.05, 0.95))), digits=2)
cd90 
ct90<-round(c(full.pc$Threat, quantile(full.pc$boot[,2], c(0.05, 0.95))), digits=2)
ct90 




###combine results from the public survey into one dataframe



dataret<-as.data.frame(cbind(c("40/50 Block", "20/30 Block","Full Sample")))
dataret$dif<-cbind(c(p4t[1], p3t[1], pt[1] ))
dataret$low<-cbind(c(p4t[2], p3t[2], pt[2] ))
dataret$up<-cbind(c(p4t[3], p3t[3], pt[3] ))
dataret$low90<-cbind(c(p4t90[2], p3t90[2], pt90[2]))
dataret$up90<-cbind(c(p4t90[3], p3t90[3], pt90[3]))


dataret$dif<-dataret$dif*100
dataret$low<-dataret$low*100
dataret$up<-dataret$up*100
dataret$low90<-dataret$low90*100
dataret$up90<-dataret$up90*100



datared<-as.data.frame(cbind(c("40/50 Block", "20/30 Block","Full Sample" )))
datared$dif<-cbind(c(p4d[1], p3d[1], pd[1] ))
datared$low<-cbind(c(p4d[2], p3d[2], pd[2]))
datared$up<-cbind(c(p4d[3], p3d[3], pd[3]))
datared$low90<-cbind(c(p4d90[2], p3d90[2], pd90[2]))
datared$up90<-cbind(c(p4d90[3], p3d90[3], pd90[3]))


datared$dif<-datared$dif*100
datared$low<-datared$low*100
datared$up<-datared$up*100
datared$low90<-datared$low90*100
datared$up90<-datared$up90*100


datacet<-as.data.frame(cbind(c("40/50 Block", "20/30 Block","Full Sample")))
datacet$dif<-cbind(c(c4t[1], c3t[1], ct[1] ))
datacet$low<-cbind(c(c4t[2], c3t[2], ct[2]))
datacet$up<-cbind(c(c4t[3], c3t[3], ct[3]))
datacet$low90<-cbind(c(c4t90[2], c3t90[2], ct90[2]))
datacet$up90<-cbind(c(c4t90[3], c3t90[3], ct90[3]))

datacet$dif<-datacet$dif*100
datacet$low<-datacet$low*100
datacet$up<-datacet$up*100
datacet$low90<-datacet$low90*100
datacet$up90<-datacet$up90*100


dataced<-as.data.frame(cbind(c("40/50 Block", "20/30 Block","Full Sample")))
dataced$dif<-cbind(c(c4d[1], c3d[1], cd[1] ))
dataced$low<-cbind(c(c4d[2], c3d[2], cd[2]))
dataced$up<-cbind(c(c4d[3], c3d[3], cd[3]))
dataced$low90<-cbind(c(c4d90[2], c3d90[2], cd90[2]))
dataced$up90<-cbind(c(c4d90[3], c3d90[3], cd90[3]))

dataced$dif<-dataced$dif*100
dataced$low<-dataced$low*100
dataced$up<-dataced$up*100
dataced$low90<-dataced$low90*100
dataced$up90<-dataced$up90*100






###################################################################################
# Figure A.1 The Relative EFfectivenss: Nuclear Foward-Deployment vs Declaratory Policy
####################################################################################



png("FigureAppendix1_FPA.png", width=10, height=6,  units = 'in', res = 300)

m<-rbind(c(1,2))
layout(m)
layout.show(2)

## Panel (a) Support for Nuclearization 
plot(NULL,                         
     xlim = c(-80, 80),                     
     ylim = c(.3, 4 + .3), 
     axes = F, xlab = NA, ylab = NA)      

abline(v = 0, lty = 2, col="grey") 


for (i in 1:length(datared$dif)) {                                          
  points(datared$dif[i], i, pch = 18, cex = 1.5)                              
  lines(c(datared$low[i], datared$up[i]), c(i, i))
  lines(c(datared$low90[i], datared$up90[i]),c(i,i), lwd=3)
  text(datared$dif[i], i, datared$V1[i], xpd = T, cex = 1, pos = 3)                    
}       

axis(side = 1)                                                                                    
mtext(side = 3, "(a) Support for Nuclearization", line = 0.9, cex=1.1)
box(col="grey")        


## Panel(b) Crediblity of Extended Deterrence

plot(NULL,                              
     xlim = c(-80, 80),                     
     ylim = c(.3, 4 + .3), 
     axes = F, xlab = NA, ylab = NA)       

abline(v = 0, lty = 2, col="grey") 

for (i in 1:length(dataced$dif)) {                                           
  points(dataced$dif[i], i, pch = 19, cex = 1.2)                              
  lines(c(dataced$low[i], dataced$up[i]), c(i, i))
  lines(c(dataced$low90[i], dataced$up90[i]),c(i,i), lwd=3)
  text(dataced$dif[i], i, dataced$V1[i], xpd = T, cex = 1, pos = 3) 
}

axis(side = 1)                                                                                         
mtext(side = 3, "(b) Credibility of Extended Deterrence ", line = 0.9, cex=1.1)

mtext(side = 1, "Changes in Support for Nuclearization \n(Deploy-Declare)(%)", line = 3, at=-220, cex=1) 
mtext(side = 1, "Changes in Credibility of Extended Deterrence \n(Deploy-Declare)(%)", line = 3, at=10, cex=1) 
box(col="grey")               


dev.off()




################################################################################
# Figure A.2. The Relative EFfectivenss: a Conditional Threat of Punishment vs Declaratory Policy
#################################################################################




png("FigureAppendix2_FPA.png", width=10, height=6,  units = 'in', res = 300)


m<-rbind(c(1,2))
layout(m)

#Panel(a) Support for Nuclearization 


plot(NULL,                              
     xlim = c(-80, 80),                     
     ylim = c(.3, 4 + .3), 
     axes = F, xlab = NA, ylab = NA)       

abline(v = 0, lty = 2, col="grey") 

for (i in 1:length(dataret$dif)) {                                           
  points(dataret$dif[i], i, pch = 18, cex = 1.5)                             
  lines(c(dataret$low[i], dataret$up[i]), c(i, i))
  lines(c(dataret$low90[i], dataret$up90[i]),c(i,i), lwd=3)
  text(dataret$dif[i], i, dataret$V1[i], xpd = T, cex = 1, pos = 3)                    
}       

axis(side = 1)                                                                                         
mtext(side = 3, "(a) Support for Nuclearization", line = 0.9, cex=1.1)
box(col="grey")        


#Panel(b) Credibility of Extended Deterrence

plot(NULL,                             
     xlim = c(-80, 80),                     
     ylim = c(.3, 4 + .3), 
     axes = F, xlab = NA, ylab = NA)      

abline(v = 0, lty = 2, col="grey") 

for (i in 1:length(datacet$dif)) {                                           
  points(datacet$dif[i], i, pch = 19, cex = 1.2)                             
  lines(c(datacet$low[i], datacet$up[i]), c(i, i))
  lines(c(datacet$low90[i], datacet$up90[i]),c(i,i), lwd=3)
  text(datacet$dif[i], i, datacet$V1[i], xpd = T, cex = 1, pos = 3) 
}


axis(side = 1)                                                                                        
mtext(side = 3, "(b) Credibility of Extended Deterrence ", line = 0.9, cex=1.1)

mtext(side = 1, "Changes in Support for Nuclearization \n(Threat-Declare)(%)", line = 3, at=-220, cex=1) 
mtext(side = 1, "Changes in Credibility of Extended Deterrence \n(Threat-Declare)(%)", line = 3, at=10, cex=1) 
box(col="grey")               


dev.off()






